package com.codes;

import com.utils.VerifyUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.UUID;

/**
 * 基础api
 */
@RestController
@Api(value = "sql api", tags = "sql api")
public class ApiController extends com.tim.controller.ApiController {
    @ApiOperation("获取单个数据")
    public Object getItem(Map<String, Object> param, String groups, String codes) throws Throwable {
        return super.item(param, groups, codes);
    }

    @ApiOperation("获取列表")
    public Object getList(Map<String, Object> param, String groups, String codes) throws Throwable {
        return super.list(param, groups, codes);
    }

    @Override
    @ApiOperation("更新删除")
    public Object exeSql(Map<String, Object> param, String groups, String codes) throws Throwable {
        return super.exeSql(param, groups, codes);
    }

    @ApiOperation("获取分页")
    public Object getPage(Map<String, Object> param, String groups, String codes) throws Throwable {
        return super.page(param, groups, codes);
    }

    @PostMapping(value = "/api/update/{tableName}/{keyName}", produces = {"application/json"}, consumes = {
            "application/json"})
    @ApiOperation("万能根据单条件更新数据")
    public Object update(@RequestBody Map<String, Object> param, @PathVariable(name = "tableName", required = true) String tableName, @PathVariable(name = "tableName", required = true) String keyName) throws Throwable {
        try {
            apiSql.updateTableMap(param, getNickNameTable(tableName), keyName);
        } catch (Exception e) {
            return e.getMessage();
        }
        return doOutData("0000");
    }

    @PostMapping(value = "/api/del/{tableName}/{keyName}/{keyValue}", produces = {"application/json"}, consumes = {
            "application/json"})
    @ApiOperation("万能根据单条件删除数据")
    public Object del(@RequestBody Map<String, Object> param, @PathVariable(name = "tableName", required = true) String tableName,
                      @PathVariable(name = "keyName", required = true) String keyName,
                      @PathVariable(name = "keyValue", required = true) String keyValue) throws Throwable {
        try {
            apiSql.del(getNickNameTable(tableName), keyName, keyValue);
        } catch (Exception e) {
            return e.getMessage();
        }
        return doOutData("0000");
    }

    @PostMapping(value = "/api/insert/{tableName}", produces = {"application/json"}, consumes = {
            "application/json"})
    @ApiOperation("万能根据单条件插入数据")
    public Object insert(@RequestBody Map<String, Object> param, @PathVariable(name = "tableName", required = true) String tableName) throws Throwable {
        try {
            param.put("id", UUID.randomUUID().toString());
            param.put("cTime", VerifyUtil.getDate());
            apiSql.insertTableMapNotKey(param,tableName);
        } catch (Exception e) {
            return e.getMessage();
        }
        return doOutData("0000");
    }
    public String getNickNameTable(String tableNickName){
        Map<String, Object> item = apiSql.item("table_nick_name", "tableNickName", tableNickName);
        if(VerifyUtil.isEmpty(item))doOutError("未找到对应的表");
        return item.get("tableNameOld")+"";
    }
}
